package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.accounting.entity.GaProductSummaryTransition;
import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.atom.service.IDealCustBatchService;
import com.jrx.anytxn.atom.service.IDealProductSummaryService;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerFirstLevelMapper;
import com.jrx.anytxn.investor.entity.*;
import com.jrx.anytxn.investor.mapper.ext.*;
import com.jrx.anytxn.transaction.config.CustBatchConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.*;
import com.jrx.anytxn.transaction.mapper.ext.*;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @program: anytxn-cf-parent
 * @description:
 * @author: xiaoyu.jing
 * @date: 2020-09-04 10:05
 **/
@Service
public class DealCustBatchServiceImpl implements IDealCustBatchService {
    Logger logger = LoggerFactory.getLogger(DealCustBatchServiceImpl.class);
    @Autowired
    private CustBatchConfig custBatchConfig;
    @Resource
    private ExtCmCustomerFirstLevelMapper extCmCustomerFirstLevelMapper;
    @Resource
    private ExtAmInterestAccrualInfoMapper extAmInterestAccrualInfoMapper;
    @Resource
    private ExtAmAccountMainInfoMapper extAmAccountMainInfoMapper;
    @Resource
    private ExtAmRepaymentPlanInfoMapper extAmRepaymentPlanInfoMapper;
    @Resource
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;
    @Resource
    private ExtTlTransactionInfoMapper extTlTransactionInfoMapper;
    @Resource
    private ExtTlGlInterfaceInfoMapper extTlGlInterfaceInfoMapper;
    @Resource
    private ExtAmDelinquencyInfoMapper extAmDelinquencyInfoMapper;
    @Resource
    private ExtAmWaiveRepaymentPlanInfoMapper extAmWaiveRepaymentPlanInfoMapper;
    @Resource
    private ExtTlInterestAccrualLogMapper extTlInterestAccrualLogMapper;
    @Resource
    private ExtAmLoanAccountInvestorInfoMapper extAmLoanAccountInvestorInfoMapper;
    @Resource
    private ExtAmAccountMainInvestorInfoMapper extAmAccountMainInvestorInfoMapper;
    @Resource
    private ExtAmRepaymentPlanInvestorInfoMapper extAmRepaymentPlanInvestorInfoMapper;
    @Resource
    private ExtAmInterestAccrualInvestorInfoMapper extAmInterestAccrualInvestorInfoMapper;
    @Resource
    private ExtAmDelinquencyInvestorInfoMapper extAmDelinquencyInvestorInfoMapper;
    @Resource
    private ExtTlInvestorInterestAccrualLogMapper extTlInvestorInterestAccrualLogMapper;
    @Resource
    private ExtTlGlInterfaceInvestorInfoMapper extTlGlInterfaceInvestorInfoMapper;
    @Autowired
    private ExtTlInvestorLoanChangeMapper extTlInvestorLoanChangeMapper;
    @Autowired
    private RedisTemplate<String, Object> template;
    @Autowired
    private IDealProductSummaryService dealProductSummaryService;
    @Autowired
    private List<BaseBatchAccoutHandle> batchAccoutHandleList;
    @Resource
    private ExtTlLoanClassifyChangeMapper extTlLoanClassifyChangeMapper;
    @Resource
    private ExtTlCustomerBlockCodeMapper extTlCustomerBlockCodeMapper;



    /**
     * 客户账务处理
     *
     * @param firstLevel
     * @return
     */
    @Override
    public CustBatchSaveBean dealCustBatch(CmCustomerFirstLevel firstLevel) throws TxnException {
        CustBatchSaveBean returnBean = null;
        try {
            CustBatchSaveBean custBatchSaveBean = new CustBatchSaveBean();
            custBatchSaveBean.setDealCust(firstLevel);
            CustBatchHelper.setCustBatchLocal(custBatchSaveBean);

            String batchType = custBatchConfig.getBatchType();
            String skipBatch = custBatchConfig.getSkipBatch();
            if (StringUtils.isBlank(batchType)) {
                throw new TxnException("batchType未配置");
            }

            Predicate<BaseBatchAccoutHandle> skip = handle -> {
                Class clazz = handle.getClass();
                if (!clazz.isAnnotationPresent(Order.class)) {
                    return true;
                }
                Order order = (Order) clazz.getAnnotation(Order.class);
                if (StringUtils.isNotBlank(skipBatch) && Arrays.asList(skipBatch.split(TransBizConstant.SPLIT)).contains(Integer.toString(order.value()))) {
                    return false;
                }
                return true;
            };


            batchAccoutHandleList.stream()
                    .filter(handle -> Arrays.asList(batchType.split(TransBizConstant.SPLIT)).contains(handle.getBatchType()))
                    .filter(skip)
                    .forEach(
                            handle -> {
                                try {
                                    logger.info("dealCustBatch:handle->{},tenantId->{},customerId->{}",handle.getClass().getSimpleName(),firstLevel.getTenantId(),firstLevel.getCustomerId());
                                    handle.afterHandle(handle.handBatchAccout());
                                } catch (Exception e) {
                                    logger.error(" ERROR dealCustBatch ERROR:handle->{},tenantId->{},customerId->{}",handle.getClass().getSimpleName(),firstLevel.getTenantId(),firstLevel.getCustomerId());
                                    throw new RuntimeException(e);
                                }
                            }
                    );
            returnBean = CustBatchHelper.getCustBatchLocal();
        }finally {
            CustBatchHelper.clearCustBatchLocal();
        }
        return returnBean;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveCustBean(CustBatchSaveBean item) {
        if (null != item.getUpdateCust()) {
            extCmCustomerFirstLevelMapper.updateByIdAndCustomerIdAndTenantId(item.getUpdateCust());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateLoanAccountList())) {
            extAmLoanAccountInfoMapper.batchUpdateLoanAccount(item.getUpdateLoanAccountList());
        }

        if (CollectionUtils.isNotEmpty(item.getSavePlanList())) {
            extAmRepaymentPlanInfoMapper.insertAmRepaymentPlanInfoList(item.getSavePlanList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdatePlanList())) {
            extAmRepaymentPlanInfoMapper.batchUpdatePlan(item.getUpdatePlanList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveWaivePlanList())) {
            extAmWaiveRepaymentPlanInfoMapper.insertAmWaiveRepaymentPlanInfoList(item.getSaveWaivePlanList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateWaivePlanList())) {
            extAmWaiveRepaymentPlanInfoMapper.batchUpdatePlan(item.getUpdateWaivePlanList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveAcctList())) {
            extAmAccountMainInfoMapper.batchInsertAccount(item.getSaveAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateAcctList())) {
            extAmAccountMainInfoMapper.batchUpdateAccount(item.getUpdateAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveAccuAcctList())) {
            extAmInterestAccrualInfoMapper.batchInsertInterestAccrualInfo(item.getSaveAccuAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateAccuAcctList())) {
            extAmInterestAccrualInfoMapper.batchUpdateInterestAccrualInfo(item.getUpdateAccuAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveDelqAcctList())) {
            extAmDelinquencyInfoMapper.batchInsertAccount(item.getSaveDelqAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateDelqAcctList())) {
            extAmDelinquencyInfoMapper.batchUpdateAccount(item.getUpdateDelqAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveGlList())) {
            extTlGlInterfaceInfoMapper.batchInsertSelective(item.getSaveGlList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveClassifyList())) {
            extTlLoanClassifyChangeMapper.batchInsert(item.getSaveClassifyList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveTlTransList())) {
            extTlTransactionInfoMapper.batchInsert(item.getSaveTlTransList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveIntrAccrualLogList())) {
            extTlInterestAccrualLogMapper.batchInsert(item.getSaveIntrAccrualLogList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateInvestorLoanAccountList())) {
            extAmLoanAccountInvestorInfoMapper.batchUpdate(item.getUpdateInvestorLoanAccountList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveInvestorPlanList())) {
            extAmRepaymentPlanInvestorInfoMapper.batchInsertRepaymentPlanInfoList(item.getSaveInvestorPlanList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateInvestorPlanList())) {
            extAmRepaymentPlanInvestorInfoMapper.batchUpdate(item.getUpdateInvestorPlanList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveInvestorAcctList())) {
            extAmAccountMainInvestorInfoMapper.batchInsertAccount(item.getSaveInvestorAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateInvestorAcctList())) {
            extAmAccountMainInvestorInfoMapper.batchUpdate(item.getUpdateInvestorAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveInvestorAccuAcctList())) {
            extAmInterestAccrualInvestorInfoMapper.batchInsertInterestAccrualInvestorInfo(item.getSaveInvestorAccuAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateInvestorAccuAcctList())) {
            extAmInterestAccrualInvestorInfoMapper.batchUpdate(item.getUpdateInvestorAccuAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveInvestorDelqAcctList())) {
            extAmDelinquencyInvestorInfoMapper.batchInsertDelinquency(item.getSaveInvestorDelqAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getUpdateInvestorDelqAcctList())) {
            extAmDelinquencyInvestorInfoMapper.batchUpdate(item.getUpdateInvestorDelqAcctList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveInvestorGlList())) {
            extTlGlInterfaceInvestorInfoMapper.batchInsertGl(item.getSaveInvestorGlList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveLoanChangeList())) {
            extTlInvestorLoanChangeMapper.batchInsert(item.getSaveLoanChangeList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveInvestorIntrAccrualLogList())) {
            extTlInvestorInterestAccrualLogMapper.batchInsertInvestorInterestAccrualLog(item.getSaveInvestorIntrAccrualLogList());
        }

        if (CollectionUtils.isNotEmpty(item.getSaveBlockList())) {
            extTlCustomerBlockCodeMapper.batchInsert(item.getSaveBlockList());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveCustBeanForBatch(List<CustBatchSaveBean> list) {
        List<CmCustomerFirstLevel> updateCustList = new ArrayList<>();
        List<AmLoanAccountInfo> updateLoanAccountList = new ArrayList<>();
        List<AmRepaymentPlanInfo> savePlanList = new ArrayList<>();
        List<AmRepaymentPlanInfo> updatePlanList = new ArrayList<>();
        List<AmWaiveRepaymentPlanInfo> saveWaivePlanList = new ArrayList<>();
        List<AmWaiveRepaymentPlanInfo> updateWaivePlanList = new ArrayList<>();
        List<AmAccountMainInfo> saveAcctList = new ArrayList<>();
        List<AmAccountMainInfo> updateAcctList = new ArrayList<>();
        List<AmInterestAccrualInfo> saveAccuAcctList = new ArrayList<>();
        List<AmInterestAccrualInfo> updateAccuAcctList = new ArrayList<>();
        List<AmDelinquencyInfo> saveDelqAcctList = new ArrayList<>();
        List<AmDelinquencyInfo> updateDelqAcctList = new ArrayList<>();
        List<TlGlInterfaceInfo> saveGlList = new ArrayList<>();
        List<TlLoanClassifyChange> saveClassifyList = new ArrayList<>();
        List<TlTransactionInfo> saveTlTransList = new ArrayList<>();
        List<TlInterestAccrualLog> saveIntrAccrualLogList = new ArrayList<>();
        List<AmLoanAccountInvestorInfo> updateInvestorLoanAccountList = new ArrayList<>();
        List<AmRepaymentPlanInvestorInfo> saveInvestorPlanList = new ArrayList<>();
        List<AmRepaymentPlanInvestorInfo> updateInvestorPlanList = new ArrayList<>();
        List<AmAccountMainInvestorInfo> saveInvestorAcctList = new ArrayList<>();
        List<AmAccountMainInvestorInfo> updateInvestorAcctList = new ArrayList<>();
        List<AmInterestAccrualInvestorInfo> saveInvestorAccuAcctList = new ArrayList<>();
        List<AmInterestAccrualInvestorInfo> updateInvestorAccuAcctList = new ArrayList<>();
        List<AmDelinquencyInvestorInfo> saveInvestorDelqAcctList = new ArrayList<>();
        List<AmDelinquencyInvestorInfo> updateInvestorDelqAcctList = new ArrayList<>();
        List<TlGlInterfaceInvestorInfo> saveInvestorGlList = new ArrayList<>();
        List<TlInvestorLoanChange> saveLoanChangeList = new ArrayList<>();
        List<TlInvestorInterestAccrualLog> saveInvestorInterestAccrualLog = new ArrayList<>();
        List<TlCustomerBlockCode> saveTlCustomerBlockCode = new ArrayList<>();
        for (CustBatchSaveBean item : list) {
            if (null != item.getUpdateCust()) {
                updateCustList.add(item.getUpdateCust());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateLoanAccountList())) {
                updateLoanAccountList.addAll(item.getUpdateLoanAccountList());
            }

            if (CollectionUtils.isNotEmpty(item.getSavePlanList())) {
                savePlanList.addAll(item.getSavePlanList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdatePlanList())) {
                updatePlanList.addAll(item.getUpdatePlanList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveWaivePlanList())) {
                saveWaivePlanList.addAll(item.getSaveWaivePlanList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateWaivePlanList())) {
                updateWaivePlanList.addAll(item.getUpdateWaivePlanList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveAcctList())) {
                saveAcctList.addAll(item.getSaveAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateAcctList())) {
                updateAcctList.addAll(item.getUpdateAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveAccuAcctList())) {
                saveAccuAcctList.addAll(item.getSaveAccuAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateAccuAcctList())) {
                updateAccuAcctList.addAll(item.getUpdateAccuAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveDelqAcctList())) {
                saveDelqAcctList.addAll(item.getSaveDelqAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateDelqAcctList())) {
                updateDelqAcctList.addAll(item.getUpdateDelqAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveGlList())) {
                saveGlList.addAll(item.getSaveGlList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveClassifyList())){
                saveClassifyList.addAll(item.getSaveClassifyList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveTlTransList())) {
                saveTlTransList.addAll(item.getSaveTlTransList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveIntrAccrualLogList())) {
                saveIntrAccrualLogList.addAll(item.getSaveIntrAccrualLogList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateInvestorLoanAccountList())) {
                updateInvestorLoanAccountList.addAll(item.getUpdateInvestorLoanAccountList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveInvestorPlanList())) {
                saveInvestorPlanList.addAll(item.getSaveInvestorPlanList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateInvestorPlanList())) {
                updateInvestorPlanList.addAll(item.getUpdateInvestorPlanList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveInvestorAcctList())) {
                saveInvestorAcctList.addAll(item.getSaveInvestorAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateInvestorAcctList())) {
                updateInvestorAcctList.addAll(item.getUpdateInvestorAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveInvestorAccuAcctList())) {
                saveInvestorAccuAcctList.addAll(item.getSaveInvestorAccuAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateInvestorAccuAcctList())) {
                updateInvestorAccuAcctList.addAll(item.getUpdateInvestorAccuAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveInvestorDelqAcctList())) {
                saveInvestorDelqAcctList.addAll(item.getSaveInvestorDelqAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getUpdateInvestorDelqAcctList())) {
                updateInvestorDelqAcctList.addAll(item.getUpdateInvestorDelqAcctList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveInvestorGlList())) {
                saveInvestorGlList.addAll(item.getSaveInvestorGlList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveLoanChangeList())) {
                saveLoanChangeList.addAll(item.getSaveLoanChangeList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveInvestorIntrAccrualLogList())) {
                saveInvestorInterestAccrualLog.addAll(item.getSaveInvestorIntrAccrualLogList());
            }

            if (CollectionUtils.isNotEmpty(item.getSaveBlockList())){
                saveTlCustomerBlockCode.addAll(item.getSaveBlockList());
            }
        }

        if (CollectionUtils.isNotEmpty(updateCustList)) {
            extCmCustomerFirstLevelMapper.batchUpdateFirstLevelForBatch(updateCustList);
        }

        if (CollectionUtils.isNotEmpty(updateLoanAccountList)) {
            extAmLoanAccountInfoMapper.batchUpdateLoanAccountForBatch(updateLoanAccountList);
        }

        if (CollectionUtils.isNotEmpty(savePlanList)) {
            extAmRepaymentPlanInfoMapper.insertAmRepaymentPlanInfoList(savePlanList);
        }

        if (CollectionUtils.isNotEmpty(updatePlanList)) {
            extAmRepaymentPlanInfoMapper.batchUpdatePlanForBatch(updatePlanList);
        }

        if (CollectionUtils.isNotEmpty(saveWaivePlanList)) {
            extAmWaiveRepaymentPlanInfoMapper.insertAmWaiveRepaymentPlanInfoList(saveWaivePlanList);
        }

        if (CollectionUtils.isNotEmpty(updateWaivePlanList)) {
            extAmWaiveRepaymentPlanInfoMapper.batchUpdatePlanForBatch(updateWaivePlanList);
        }

        if (CollectionUtils.isNotEmpty(saveAcctList)) {
            extAmAccountMainInfoMapper.batchInsertAccount(saveAcctList);
        }

        if (CollectionUtils.isNotEmpty(updateAcctList)) {
            extAmAccountMainInfoMapper.batchUpdateAccountForBatch(updateAcctList);
        }

        if (CollectionUtils.isNotEmpty(saveAccuAcctList)) {
            extAmInterestAccrualInfoMapper.batchInsertInterestAccrualInfo(saveAccuAcctList);
        }

        if (CollectionUtils.isNotEmpty(updateAccuAcctList)) {
            extAmInterestAccrualInfoMapper.batchUpdateInterestAccrualInfoForBatch(updateAccuAcctList);
        }

        if (CollectionUtils.isNotEmpty(saveDelqAcctList)) {
            extAmDelinquencyInfoMapper.batchInsertAccount(saveDelqAcctList);
        }

        if (CollectionUtils.isNotEmpty(updateDelqAcctList)) {
            extAmDelinquencyInfoMapper.batchUpdateAccountForBatch(updateDelqAcctList);
        }

        if (CollectionUtils.isNotEmpty(saveGlList)) {
            extTlGlInterfaceInfoMapper.batchInsertSelective(saveGlList);
        }

        if (CollectionUtils.isNotEmpty(saveClassifyList)) {
            extTlLoanClassifyChangeMapper.batchInsert(saveClassifyList);
        }

        if (CollectionUtils.isNotEmpty(saveTlTransList)) {
            extTlTransactionInfoMapper.batchInsert(saveTlTransList);
        }

        if (CollectionUtils.isNotEmpty(saveIntrAccrualLogList)) {
            extTlInterestAccrualLogMapper.batchInsert(saveIntrAccrualLogList);
        }

        if (CollectionUtils.isNotEmpty(updateInvestorLoanAccountList)) {
            extAmLoanAccountInvestorInfoMapper.batchUpdateLoanAccountForBatch(updateInvestorLoanAccountList);
        }

        if (CollectionUtils.isNotEmpty(saveInvestorPlanList)) {
            extAmRepaymentPlanInvestorInfoMapper.batchInsertRepaymentPlanInfoList(saveInvestorPlanList);
        }

        if (CollectionUtils.isNotEmpty(updateInvestorPlanList)) {
            extAmRepaymentPlanInvestorInfoMapper.batchUpdatePlanForBatch(updateInvestorPlanList);
        }

        if (CollectionUtils.isNotEmpty(saveInvestorAcctList)) {
            extAmAccountMainInvestorInfoMapper.batchInsertAccount(saveInvestorAcctList);
        }

        if (CollectionUtils.isNotEmpty(updateInvestorAcctList)) {
            extAmAccountMainInvestorInfoMapper.batchUpdateAccountMainInfoForBatch(updateInvestorAcctList);
        }

        if (CollectionUtils.isNotEmpty(saveInvestorAccuAcctList)) {
            extAmInterestAccrualInvestorInfoMapper.batchInsertInterestAccrualInvestorInfo(saveInvestorAccuAcctList);
        }

        if (CollectionUtils.isNotEmpty(updateInvestorAccuAcctList)) {
            extAmInterestAccrualInvestorInfoMapper.batchUpdateInterestAccrualInfoForBatch(updateInvestorAccuAcctList);
        }

        if (CollectionUtils.isNotEmpty(saveInvestorDelqAcctList)) {
            extAmDelinquencyInvestorInfoMapper.batchInsertDelinquency(saveInvestorDelqAcctList);
        }

        if (CollectionUtils.isNotEmpty(updateInvestorDelqAcctList)) {
            extAmDelinquencyInvestorInfoMapper.batchUpdateDelinquencyForBatch(updateInvestorDelqAcctList);
        }

        if (CollectionUtils.isNotEmpty(saveInvestorGlList)) {
            extTlGlInterfaceInvestorInfoMapper.batchInsertGl(saveInvestorGlList);
        }

        if (CollectionUtils.isNotEmpty(saveLoanChangeList)) {
            extTlInvestorLoanChangeMapper.batchInsert(saveLoanChangeList);
        }

        if (CollectionUtils.isNotEmpty(saveInvestorInterestAccrualLog)) {
            extTlInvestorInterestAccrualLogMapper.batchInsertInvestorInterestAccrualLog(saveInvestorInterestAccrualLog);
        }

        if (CollectionUtils.isNotEmpty(saveTlCustomerBlockCode)) {
            extTlCustomerBlockCodeMapper.batchInsert(saveTlCustomerBlockCode);
        }
    }

    @Override
    public CustBatchSaveBean dealAlreadyCustBatch(CmCustomerFirstLevel firstLevel) {
        String customerId = firstLevel.getCustomerId();
        String tenantId = firstLevel.getTenantId();
        List<AmLoanAccountInfo> dealLoanAcctList = extAmLoanAccountInfoMapper
                .selectByCustAndChannelAndStatus(customerId, null, tenantId, Arrays.asList(TransBizConstant.LOAN_STATUS_1, TransBizConstant.LOAN_STATUS_2));
        if (CollectionUtils.isNotEmpty(dealLoanAcctList)) {
            for (AmLoanAccountInfo loan : dealLoanAcctList) {
                String key = String.format(TransBizConstant.PRODUCT_SUMMARY_LOAN_REDIS_KEY, loan.getTenantId(), loan.getLoanId());
                ValueOperations<String, Object> redis = template.opsForValue();
                Object obj = redis.get(key);
                if (null == obj) {
                    continue;
                }
                GaProductSummaryTransition transition = (GaProductSummaryTransition) obj;
                dealProductSummaryService.putTransition(transition);
                //如果发生异常，重跑批量可能上面循环的金额会重复计算，如果已更新内存和redis的总额，在这里删除掉该订单的总分核对金额
                template.delete(key);
            }
        }
        CustBatchSaveBean custBatchSaveBean = new CustBatchSaveBean();
        custBatchSaveBean.setDealCust(firstLevel);
        return custBatchSaveBean;
    }
}
